from django.db import models

from apps.organizations.models import Teacher
from apps.users.models import BaseModel

# 1.设计表结构
"""
实体1<关系>实体2
课程  章节  视频  课程讲师
"""


class Course(BaseModel):
    """课程类"""
    teacher = models.ForeignKey(Teacher, verbose_name="讲师", on_delete=models.CASCADE)
    name = models.CharField("课程名", max_length=50)
    desc = models.CharField("课程描述", max_length=300)
    learn_time = models.IntegerField("学习时长（分钟数）", default=0)
    degree = models.CharField("难度", choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")), max_length=2)
    students = models.IntegerField("学习人数", default=0)
    fav_nums = models.IntegerField("收藏人数", default=0)
    click_nums = models.IntegerField("点击人数", default=0)
    category = models.CharField("课程类别", max_length=20, default=u"后端开发")
    tag = models.CharField("标签", max_length=10, default="")
    need_know = models.CharField("课程须知", default="", max_length=300)
    teacher_tell = models.CharField("老师告诉你", default="", max_length=300)

    detail = models.TextField("课程详情")
    image = models.ImageField("封面图", upload_to="courses/%Y/%m", max_length=100)

    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Lesson(BaseModel):
    """章节类"""
    course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.CASCADE)
    name = models.CharField("章节名", max_length=100)
    learn_time = models.IntegerField(u"学习时长（分钟数）", default=0)

    class Meta:
        verbose_name = "课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Video(BaseModel):
    """视频类"""
    lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
    name = models.CharField("视频名", max_length=100)
    learn_time = models.IntegerField(u"学习时长（分钟数）", default=0)
    url = models.CharField("播放地址", max_length=200)

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseResource(BaseModel):
    """课程资源类"""
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    name = models.CharField("名称", max_length=100)
    file = models.FileField("下载地址", max_length=200, upload_to="course/resourse/%Y/%m")

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
